import json
from apps.merchant.item.dao import ItemDao
from apps.user.dao import UserDao
from apps.pay.recordIncomes.dao import RecordIncomesDao
from decimal import Decimal
from apps.system.lessee.dao import LesseeDao
from project.config import selfSendContributionValueRate
def countPersonalAwardHandle(order):
    _order = order
    print('发放个个人奖励', order.content)
    content = json.loads(_order.content)
    # 赠送购买者锁定积分
    selfSendIncomesLockeds = 0
    # 直推购物积分
    verticalSendIncomes = 0
    # 日释放池
    freePoolDays = 0
    # 月释放池
    freePoolMonths = 0
    # 运营补贴
    operationAwards = 0
    # 供应链奖励
    supplyAwards = 0
    # 助残基金
    helpFunds = 0
    # 毛利润
    grossProfits = 0

    for orderItem in content.get('orderItemList'):
        selfSendIncomesLockeds = selfSendIncomesLockeds + Decimal(orderItem.get('selfSendIncomesLocked'))*Decimal(orderItem.get('payCount'))
        verticalSendIncomes = verticalSendIncomes + Decimal(orderItem.get('verticalSendIncomes'))*Decimal(orderItem.get('payCount'))
        freePoolDays = freePoolDays + Decimal(orderItem.get('dayAward'))*Decimal(orderItem.get('payCount'))
        freePoolMonths = freePoolMonths + Decimal(orderItem.get('monthAward'))*Decimal(orderItem.get('payCount'))
        operationAwards = operationAwards + Decimal(orderItem.get('operationAward'))*Decimal(orderItem.get('payCount'))
        supplyAwards = supplyAwards + Decimal(orderItem.get('supplyAward'))*Decimal(orderItem.get('payCount'))
        helpFunds = helpFunds + Decimal(orderItem.get('helpFund'))*Decimal(orderItem.get('payCount'))
        grossProfits = grossProfits + Decimal(orderItem.get('grossProfit'))*Decimal(orderItem.get('payCount'))

    # 赠送贡献点
    payway = 'contributionValue'
    sellingPrice = _order.sellingPrice
    sendContributionValues = int(sellingPrice / selfSendContributionValueRate)
    UserDao().addWallet(payway, _order.user, sendContributionValues)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = _order.user.id
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = sendContributionValues
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)

    # 赠送上级贡献点
    payway = 'contributionValue'
    if _order.user.parent:
        verticalSendContributionValues = int(sendContributionValues / 2)
        UserDao().addWallet(payway, _order.user.parent, verticalSendContributionValues)
        data = {}
        data['scene'] = 'verticalAward'
        data['user'] = _order.user.parent.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = verticalSendContributionValues
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)

    # 赠送锁定积分
    payway = 'incomesLocked'
    UserDao().addWallet(payway, _order.user, selfSendIncomesLockeds)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = _order.user.id
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = selfSendIncomesLockeds
    data['moneyLocked'] = selfSendIncomesLockeds
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)

    # 赠送原积分转换为锁定积分
    if _order.user.incomesOriginal > 0:
        selfSendIncomesLockedsForOriginal = 0
        if _order.user.incomesOriginal < selfSendIncomesLockeds:
            selfSendIncomesLockedsForOriginal = _order.user.incomesOriginal
        else:
            selfSendIncomesLockedsForOriginal = selfSendIncomesLockeds
        # 减少原积分
        payway = 'incomesOriginal'
        UserDao().subWallet(payway, _order.user, selfSendIncomesLockedsForOriginal)
        data = {}
        data['scene'] = 'fromOriginal'
        data['user'] = _order.user.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = selfSendIncomesLockedsForOriginal
        data['payway'] = payway
        data['changeWay'] = 'sub'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)

        payway = 'incomesLocked'
        UserDao().addWallet(payway, _order.user, selfSendIncomesLockedsForOriginal)
        data = {}
        data['scene'] = 'fromOriginal'
        data['user'] = _order.user.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = selfSendIncomesLockedsForOriginal
        data['moneyLocked'] = selfSendIncomesLockedsForOriginal
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)
    
    
    
    # 赠送上级购物积分
    payway = 'incomes'
    if _order.user.parent:
        UserDao().addWallet(payway, _order.user.parent, verticalSendIncomes)
        data = {}
        data['scene'] = 'verticalAward'
        data['user'] = _order.user.parent.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = verticalSendIncomes
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)

    # 日释放池
    payway = 'freePoolDay'
    LesseeDao().addWallet(payway, freePoolDays)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = freePoolDays
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 月释放池
    payway = 'freePoolMonth'
    LesseeDao().addWallet(payway, freePoolMonths)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = freePoolMonths
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 运营补贴池
    payway = 'operationAwardPool'
    LesseeDao().addWallet(payway, operationAwards)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = operationAwards
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 助残基金池
    payway = 'helpFundPool'
    LesseeDao().addWallet(payway, helpFunds)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = helpFunds
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 供应链奖励
    payway = 'supplyAwards'
    LesseeDao().addWallet(payway, supplyAwards)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = supplyAwards
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 毛利润
    payway = 'grossProfit'
    LesseeDao().addWallet(payway, grossProfits)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = grossProfits
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)

    return sendContributionValues